package com.itheima.tanhua.admin.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.model.domain.Logs;
import com.itheima.model.vo.MovementsHVo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.PageResultM;
import com.itheima.model.vo.UserVo;
import com.itheima.tanhua.admin.service.ManageService;
import org.apache.commons.lang3.RandomUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangpei
 * @ClassName ManageController
 * @date 2022/6/21/13:45
 */
@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 用户展示
     */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize,
                                Long id, String nickname, String city) {
        PageResult pageResult = manageService.users(page, pagesize, id, nickname, city);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 用户基本资料
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity baseInfo(@PathVariable Long userID) { //被查看的用户的Id
        UserVo userVo = manageService.baseInfo(userID);
        return ResponseEntity.ok(userVo);
    }

    /**
     * 冻结用户
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map map) {
        boolean flag = manageService.freeze(map);
        HashMap<String, String> massageMap = new HashMap<>();
        massageMap.put("message", flag == true ? "操作成功" : "操作失败");
        return ResponseEntity.ok(massageMap);
    }

    /**
     * 解冻用户
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map) {
        boolean flag = manageService.unfreeze(map);
        HashMap<String, String> massageMap = new HashMap<>();
        massageMap.put("message", flag == true ? "操作成功" : "操作失败");
        return ResponseEntity.ok(massageMap);
    }

    @Autowired
    private ManageService messagesService;

    //探花后台mastage /messages/pass
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] item) {


        Map<String, String> map = messagesService.pass(item);


        return ResponseEntity.ok(map);
    }

    //探花后台拒绝策略
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] item) {

        Map<String, String> map = messagesService.reject(item);

        return ResponseEntity.ok(map);


    }

    //探花后台撤销策略
    @PostMapping("/messages/revocation")
    public ResponseEntity revocation(@RequestBody String[] item) {

        Map<String, String> map = messagesService.revocation(item);
        return ResponseEntity.ok(map);
    }


    //探花后台消息详情接口
    @GetMapping("/messages/{id}")
    public ResponseEntity messages(@PathVariable Integer id) {
        //直接掉方法
        MovementsHVo movementsHVo = messagesService.messages(id);
        return ResponseEntity.ok(movementsHVo);
    }


    //消息置顶
    @PostMapping("/messages/{id}/top")
    public ResponseEntity top(@PathVariable Integer id) {
        //
        Map<String, String> map = messagesService.top(id);
        return ResponseEntity.ok(map);
    }

    //消息取消置顶
    @PostMapping("/messages/{id}/untop")
    public ResponseEntity uptop(@PathVariable Integer id) {
        //
        Map<String, String> map = messagesService.untop(id);
        return ResponseEntity.ok(map);
    }

    //分页查询置顶
    @GetMapping("/messages")
    public ResponseEntity messagess(int page, int pagesize, String sortProp, String sortOrder, String id, Long sd, Long ed
            , Long uid, Integer state) {

        PageResultM messages = messagesService.getmeassagelist(page, pagesize, sortProp, sortOrder, id, sd, ed, state, "Messages", uid);
        return ResponseEntity.ok(messages);
    }

//    //分页查询id
//    @GetMapping("/messages/{uid}")
//    public ResponseEntity meggs(@PathVariable Long uid,int page,int pagesize, String sortProp, String sortOrder){
//
//        PageResult messages = messagesService.getmeassagelists(page, pagesize, sortProp, sortOrder,uid);
//        return ResponseEntity.ok(messages);
//    }

    @GetMapping("/videos")
    public ResponseEntity videos(Long uid, Integer page,
                                 Integer pagesize, String sortProp, String sortOrder, Integer state) { //被查看的用户的Id
        PageResult pageResult = manageService.videos(uid, page, pagesize, sortProp, sortOrder);
        return ResponseEntity.ok(pageResult);
    }


    @GetMapping("/messages/comments")
    public ResponseEntity comments(Long messageID, Integer page, Integer pagesize, String sortProp, String sortOrder) {
        PageResult pageResult = manageService.comments(messageID, page, pagesize, sortProp, sortOrder);

        return ResponseEntity.ok(null);

    }

    @GetMapping("/logs")
    public ResponseEntity logs(Long uid, Integer page,
                               Integer pagesize, String sortProp, String sortOrder, Integer state) { //被查看的用户的Id
        ArrayList<Logs> logsArrayList = new ArrayList<>();
        String [] city ={"蜀山区","庐江区","瑶海区","包河"};
        String [] iphone ={"小米10","华为P40","iphone13PrpMax","锤子手机"};
        long time = System.currentTimeMillis();
        for (int i = 1; i < 10; i++) {
            long randTime = RandomUtils.nextLong(3600000, 36000000);
            int anInt1 = RandomUtils.nextInt(0, 3);
            int anInt2 = RandomUtils.nextInt(0, 3);
            Long id = 1L;
            time -= randTime;
            Logs logs = new Logs();
            logs.setId(id);
            logs.setEquipment(iphone[anInt2]);
            logs.setLogTime(new Date(time));
            logs.setPlace("合肥-"+city[anInt1]);
            logsArrayList.add(logs);
        }
        PageResult pageResult = new PageResult(page, pagesize, 10L, logsArrayList);
        return ResponseEntity.ok(pageResult);
    }


}
